package com.carkeeper.subject.service.impl;


import com.carkeeper.subject.dao.StoreDetailMapper;
import com.carkeeper.subject.dao.StoreMapper;
import com.carkeeper.subject.pojo.Store;
import com.carkeeper.subject.pojo.StoreDetail;
import com.carkeeper.subject.pojo.StoreFull;
import com.carkeeper.subject.service.StoreService;
import com.carkeeper.utils.IdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private StoreDetailMapper storeDetailMapper;

    /**
     * 查询所有服务商
     * @return
     */
    @Override
    public Page<Store> findAll(Integer page,Integer size) {
        //默认页码为1
        if (page==null){
            page=1;
        }
        //每页条数设置为10
        if (size==null){
            size=10;
        }
        PageHelper.startPage(page,size);
        Example example = new Example(Store.class);
        Example.Criteria criteria=example.createCriteria();
        //本平台不在服务商列表中显示
        criteria.andNotEqualTo("id","123456");
        //只有已通过审核会显示
        criteria.andBetween("status","1","3");
        return (Page<Store>) storeMapper.selectByExample(example);
    }

    /**
     * 添加服务商
     * @param storeFull
     */
    @Override
    public void add(StoreFull storeFull) {
        Store store = storeFull.getStore();
        StoreDetail storeDetail = storeFull.getStoreDetail();
        if (store==null||storeDetail==null){
            throw new RuntimeException("请上传完整商家信息");
        }
        //获取服务商编码
        String code = this.getCode();
        store.setCode(code);

        //设置id
        long id = idWorker.nextId();

        store.setId(id);
        store.setStatus("0");//状态设置为未审核
        store.setType("1");//服务商

        storeDetail.setId(id);
        //添加
        int a = storeMapper.insertSelective(store);
        int b = storeDetailMapper.insertSelective(storeDetail);

    }
    /**
     *
     * @param map 查询条件
     * @param page  当前页码
     * @param size  每页条数
     * @return  返回分页结果
     *//*
    @Override
    public Page<Store> findPage(Map map, int page, int size) {
        //获取查询条件
        Example example = createExample(map);
        //设置分页
        PageHelper.startPage(page,size);
        return (Page<Store>) storeMapper.selectByExample(example);
    }*/

    /**
     *
     * @param map 查询条件
     * @return  返回分页结果
     */
    @Override
    public Page<Store> findPage(Map<String,Object> map) {
        //设置分页参数
        Integer page=1;
        Integer size=10;
        //设置默认值为1
        if (map.get("page")!=null&&!"".equals(map.get("page"))){
            page= Integer.parseInt((String)map.get("page")) ;
        }
        if (map.get("size")!=null&&!"".equals(map.get("size"))){
            size= Integer.parseInt((String)map.get("size")) ;
        }
        //获取查询条件
        Example example = createExample(map);
        //设置分页
        PageHelper.startPage(page,size);
        return (Page<Store>) storeMapper.selectByExample(example);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public StoreFull findById(String id) {
        StoreFull storeFull = new StoreFull();
        Store store = storeMapper.selectByPrimaryKey(id);
        StoreDetail storeDetail = storeDetailMapper.selectByPrimaryKey(id);
        storeFull.setStore(store);
        storeFull.setStoreDetail(storeDetail);
        return storeFull;
    }
    /**
     * 修改
     * @param storeFull
     */
    @Override
    public void update(StoreFull storeFull) {
        Store store = storeFull.getStore();
        StoreDetail storeDetail = storeFull.getStoreDetail();
        storeDetail.setId(store.getId());

        //更新
        int a = storeMapper.updateByPrimaryKey(store);
        int b = storeDetailMapper.updateByPrimaryKey(storeDetail);

    }
    /**
     * 根据服务商Id上锁与解锁
     * @param id
     */
    @Override
    public void lockOrUnlock(String id) {
        //根据id查出服务商
        Store store = storeMapper.selectByPrimaryKey(id);
        if (store!=null){
            String status = store.getStatus();
            if ("1".equals(status)){
                //状态是未锁定
                store.setStatus("3");//改为锁定状态
            }else {
                store.setStatus("1");
            }
        }
        //更新数据
        int a = storeMapper.updateByPrimaryKey(store);
    }

        @Override
    public Page<Store> findAudited(int currPage,int pageSize) {
        PageHelper.startPage(currPage,pageSize);
        Example example = new Example(Store.class);
        example.orderBy("createTime").asc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status","0");
        Page<Store> list = (Page<Store>) storeMapper.selectByExample(example);
        return list;
    }

    @Override
    public Page<Store> findrejected(int currPage,int pageSize) {
        PageHelper.startPage(currPage,pageSize);
        Example example = new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status","2");
        Page<Store> list = (Page<Store>) storeMapper.selectByExample(example);
        return list;
    }

    @Override
    public Page<Store> indRejectedByCondition(Integer currPage, Integer pageSize,Map<String,Object> map) {
        PageHelper.startPage(currPage,pageSize);
        Example example = new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        if(map!=null&&map.size()>0){
            for (String s : map.keySet()) {
               criteria.andEqualTo(s,map.get(s));
            }
        }
        criteria.andEqualTo("status","2");
        Page<Store> list = (Page<Store>) storeMapper.selectByExample(example);
        return list;
    }

    @Override
    public Page<Store> findAuditedByCondition(Integer currPage, Integer pageSize, Map<String, Object> map) {
        PageHelper.startPage(currPage,pageSize);
        Example example = new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        if(map!=null&&map.size()>0){
            for (String s : map.keySet()) {
                criteria.andEqualTo(s,map.get(s));
            }
        }
        criteria.andEqualTo("status","0");
        Page<Store> list = (Page<Store>) storeMapper.selectByExample(example);
        return list;
    }

    @Override
    public StoreFull findStoreDetail(String code) {
        Store store = new Store();
        store.setCode(code);
        Store one = storeMapper.selectOne(store);
        StoreDetail storeDetail = new StoreDetail();
        storeDetail.setId(one.getId());
        StoreDetail one1 = storeDetailMapper.selectOne(storeDetail);
        StoreFull storeFull = new StoreFull();
        storeFull.setStore(one);
        storeFull.setStoreDetail(one1);
        return storeFull;
    }

    @Override
    public int pass(Long id) {
        Store store = storeMapper.selectByPrimaryKey(id);
        store.setStatus("1");
        int i = storeMapper.updateByPrimaryKeySelective(store);
        return i;
    }

    @Override
    public int reject(Long id) {
        Store store = storeMapper.selectByPrimaryKey(id);
        store.setStatus("2");
        int i = storeMapper.updateByPrimaryKeySelective(store);
        return i;
    }

    /**
     * 获取递增的6位数编码号
     * @return
     */
    public String getCode(){

        //序列号前缀加特定标识，如系统模块名之类的 防止重复
        String key = "Code";
        Long count = redisTemplate.opsForValue().increment(key, 1);//自增
        int a=100000;
        return (count+=a)+"";
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Store.class);
        Example.Criteria criteria = example.createCriteria();
        //排除平台
        criteria.andNotEqualTo("id","123456");
        //只有状态为已通过审核的商家才可以显示
        criteria.andBetween("status","1","3");
        if(searchMap!=null){
            // ID
            if(searchMap.get("simpleName")!=null ){
                criteria.andEqualTo("simpleName",searchMap.get("simpleName"));
            }
            // 汽修厂编码
            if(searchMap.get("code")!=null ){
                criteria.andEqualTo("code",searchMap.get("code"));
            }
            // 省市区
            if(searchMap.get("province")!=null ){
                criteria.andEqualTo("province",searchMap.get("province"));
            }
            if(searchMap.get("city")!=null ){
                criteria.andEqualTo("city",searchMap.get("city"));
            }
            if(searchMap.get("county")!=null ){
                criteria.andEqualTo("county",searchMap.get("county"));
            }
            // 联系电话
            if(searchMap.get("contactPhone")!=null ){
                criteria.andEqualTo("contactPhone",searchMap.get("contactPhone"));
            }
            // 是否连锁
            if(searchMap.get("isChain")!=null ){
                criteria.andEqualTo("isChain",searchMap.get("isChain"));
            }
            // 自动五星好评
            if(searchMap.get("status")!=null ){
                criteria.andEqualTo("status",searchMap.get("status"));
            }

        }
        return example;
    }






    /**
     * 随机生成6位数
     * @return
     */
    /*public Integer getRandomNum() {
        int flag=0;
        for (int i = 0; i <= 100; i++) {
             flag = new Random().nextInt(999999);
            if (flag < 100000) {
                flag += 100000;
            }
            System.out.println(flag);
        }
        return flag;
    }*/
}
